﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;

namespace TrenchcoatRobots.Datatypes {
    /// <summary>
    /// Audio datatype.
    /// </summary>
    class AudioSample {

        #region Properties
        public SoundEffectInstance soundEffect;
        public string Name { get; private set; }
        public bool Looping { get { return soundEffect.IsLooped; } set { soundEffect.IsLooped = value; } }
        public SoundState State { get { return soundEffect.State; } }
        public float Pitch { get { return soundEffect.Pitch; } set { soundEffect.Pitch = value; } } // -1 to 1
        public float Volume { get { return soundEffect.Volume; } set { soundEffect.Volume = value; } } // 0 to 1
        #endregion

        #region Constructors
        public AudioSample(SoundEffect effect, string n) {
            Init(effect, n);
        }
        public AudioSample(SoundEffect effect, string n, bool loop)
        {
            Init(effect, n);
            Looping = loop;
        }
        public AudioSample(SoundEffect effect, string n, bool loop, float pitch, float volume)
        {
            Init(effect, n);
            Looping = loop;
            Pitch = pitch;
            Volume = volume;
        }
        public AudioSample(SoundEffect effect, string n, bool loop, float volume)
        {
            Init(effect, n);
            Looping = loop;
            Volume = volume;
        }
        public AudioSample(SoundEffect effect, string n, float volume)
        {
            Init(effect, n);
            Volume = volume;
        }
        #endregion

        #region Methods
        public void Init(SoundEffect effect, string n) {
            soundEffect = effect.CreateInstance();
            Name = n;
        }
        /// <summary>
        /// Plays the AudioSample, if not already playing.
        /// </summary>
        public void Play() {
            if (soundEffect.State != SoundState.Playing) {
                soundEffect.Play();
            }
        }
        /// <summary>
        /// Stops playing the AudioSample, if playing or paused.
        /// </summary>
        public void Stop() {
            if (soundEffect.State != SoundState.Stopped) {
                soundEffect.Stop();
            }
        }
        /// <summary>
        /// Stops playing the AudioSample, if playing or paused, with the option to play until the end of current loop. Useful when looping a sound.
        /// </summary>
        /// <param name="immediate">Specify true to stop playing immediately, false to play until end of current loop and then stop.</param>
        public void Stop(bool immediate)
        {
            if (soundEffect.State != SoundState.Stopped)
            {
                soundEffect.Stop(immediate);
            }
        }
        /// <summary>
        /// Resumes a paused AudioSample.
        /// </summary>
        public void Resume() {
            if (soundEffect.State == SoundState.Paused) {
                soundEffect.Resume();
            }
        }
        /// <summary>
        /// Pauses a playing AudioSample.
        /// </summary>
        public void Pause() {
            if (soundEffect.State == SoundState.Playing)
            {
                soundEffect.Pause();
            }
        }
        #endregion
    }
}